/**
 * File Name   : AdminsCfgService.java
 * Description : 
 */
package com.hh.admin.admuser.service;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;

import com.hh.admin.admuser.form.AdminsCfgForm;
import com.hh.admin.admuser.form.ParamAdminsCfgBean;
import com.hh.admin.admuser.form.ResultAdminsCfgBean;
import com.hh.common.ibatis.dao.IQueryDao;
import com.hh.common.ibatis.dao.IUpdateDao;
import com.hh.common.util.StringUtil;

/**
 * @author 冯昌硕
 * @create 2012-12-20 下午9:03:41
 */
public class AdminsCfgService {

    private static final Logger log = Logger.getLogger(AdminsCfgService.class);
    
    private IQueryDao queryDao;
    
    private IUpdateDao updateDao;
    
    /**
     * 分页查询管理员用户列表
     * @param form
     * @return
     * @throws SQLException
     * @create 2012-12-20 下午11:15:06
     */
    public List<ResultAdminsCfgBean> getAdminsList(AdminsCfgForm form) throws SQLException {
        List<ResultAdminsCfgBean> resultLst = null;
        
        int currentPage = form.getCurrentPage();
        int formCurrentPage = currentPage;
        if(formCurrentPage <= 0) {
            currentPage = 1;
        }
        
        int pageSize = form.getPageSize();
        int offset = (currentPage - 1) * pageSize;
        log.debug("管理员用户查询 : formCurrentPage=[" + formCurrentPage + "] currentPage=[" + currentPage + "] pageSize=[" + pageSize + "] offset=[" + offset + "]");
        
        ParamAdminsCfgBean paramBean = new ParamAdminsCfgBean();
        paramBean.setOffset(offset);
        paramBean.setPageSize(pageSize);
        
        resultLst = queryDao.queryForObjectList("user.selAdminsList", paramBean);
        
        return resultLst;
    }
    
    /**
     * 获取管理员用户总数
     * @param form
     * @return
     * @create 2012-12-20 下午11:27:48
     */
    public int getAdminsTotleCnt(AdminsCfgForm form) {
        int totleCnt = 0;
        
        try {
            Integer rs = queryDao.queryForObject("user.selAdminsCount", Integer.class);
            if(rs != null) {
                totleCnt = rs;
            }
        } catch (ClassCastException e) {
            log.error("user.selAdminsCount 查询ClassCastException异常：" + e.toString());
            e.printStackTrace();
        } catch (SQLException e) {
            log.error("user.selAdminsCount 查询SQLException异常：" + e.toString());
            e.printStackTrace();
        }
        
        return totleCnt;
    }
    
    /**
     * 判断用户名是否惟一
     * @param form
     * @return
     * @throws ClassCastException
     * @throws SQLException
     * @create 2012-12-23 下午4:36:21
     */
    public boolean uniqueAccount(AdminsCfgForm form) throws ClassCastException, SQLException {
        String account = form.getAccount();
        String accountRs = queryDao.queryForObject("user.selUniqueAccount", account, String.class);
        if(StringUtil.isEmpty(accountRs)) {
            return true;
        }
        
        return false;
    }
    
    /**
     * 添加新管理员用户
     * @param form
     * @throws SQLException 
     * @create 2012-12-23 下午3:58:57
     */
    public Object insertAdminUser(AdminsCfgForm form) throws SQLException {
        ParamAdminsCfgBean paramObj = new ParamAdminsCfgBean();
        paramObj.setAccount(form.getAccount());
        paramObj.setPassword(form.getPassword());
        return updateDao.insert("user.insertAdminUser", paramObj);
    }
    
    /**
     * 删除指定管理员用户
     * @param account 用户账号
     * @return 删除操作影响行数
     * @throws SQLException 
     * @create 2012-12-23 下午6:28:52
     */
    public int deleteOneAdminUser(String account) throws SQLException {
        return updateDao.delete("user.deleteAdminUser", account);
    }
    
    /**
     * 批量删除用户记录
     * @param accounts 要删除的账号数组
     * @return the number of rows updated in the batch
     * @throws SQLException
     * @create 2012-12-23 下午9:48:15
     */
    public int deleteManyAdminUser(String[] accounts) throws SQLException {
        return updateDao.batchDelete("user.deleteAdminUser", accounts);
    }
    
    /**
     * 获取指定管理员用户的详情
     * @param account 管理员账号
     * @return
     * @throws SQLException 
     * @throws ClassCastException 
     * @create 2012-12-24 下午8:08:49
     */
    public ResultAdminsCfgBean getAdminDetail(String account) throws ClassCastException, SQLException {
        return queryDao.queryForObject("user.selectAdminDetail", account, ResultAdminsCfgBean.class);
    }

    public void setQueryDao(IQueryDao queryDao) {
        this.queryDao = queryDao;
    }

    public void setUpdateDao(IUpdateDao updateDao) {
        this.updateDao = updateDao;
    }
    
}
